using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json;

namespace Utils
{
    public static class IOUtil
    {
        #region Byte转16进制字符串
        public static string ByteToHex(byte[] data)
        {
            try
            {
                string str = "";
                for (int i = 0; i < data.Length; i++)
                {
                    byte tmp = data[i];
                    str += tmp.ToString("X2");
                }
                return str;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 16进制转byte[]
        public static byte[] HexToByte(string hex)
        {
            try
            {
                byte[] data = new byte[hex.Length / 2];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
                }
                return data;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 压缩字节
        public static byte[] Compress(byte[] data)
        {
            try
            {
                if (null == data)
                    return null;
                if (data.Length < 1)
                    return data;
                MemoryStream ms = new MemoryStream();
                GZipStream gs = new GZipStream(ms, CompressionMode.Compress);
                gs.Write(data, 0, data.Length);
                gs.Flush();
                gs.Close();
                byte[] retData = ms.ToArray();
                ms.Close();
                return retData;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 解压数据
        public static byte[] DeCompress(byte[] data)
        {
            try
            {
                if (data == null)
                    return null;
                if (data.Length < 1)
                    return data;
                MemoryStream ms = new MemoryStream(data);
                MemoryStream outMs = new MemoryStream();
                GZipStream gs = new GZipStream(ms, CompressionMode.Decompress);
                gs.CopyTo(outMs);
                gs.Flush();
                gs.Close();
                byte[] retData = outMs.ToArray();
                ms.Close();
                outMs.Close();
                return retData;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 序列化对象
        public static byte[] SerializeObject(object obj)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                IFormatter fmter = new BinaryFormatter();
                fmter.Serialize(ms, obj);
                byte[] retData = ms.ToArray();
                ms.Close();
                return retData;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 反序列化对象
        public static object DeserializeObject(byte[] data)
        {
            try
            {
                MemoryStream input = new MemoryStream(data);
                IFormatter fmter = new BinaryFormatter();
                object obj= fmter.Deserialize(input);
                input.Close();
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region JSON序列化
        public static string JsonSer(object obj)
        {
            try
            {
                string retStr= JsonConvert.SerializeObject(obj);
                return retStr;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region JSON反序列化
        public static object JsonDeSer(string jsonStr)
        {
            try
            {
                object obj = JsonConvert.DeserializeObject(jsonStr);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
